package com.karaoke.android.library.mvvm

import android.databinding.BaseObservable
import com.trello.rxlifecycle.LifecycleProvider
import com.trello.rxlifecycle.LifecycleTransformer
import com.trello.rxlifecycle.RxLifecycle
import com.trello.rxlifecycle.android.ActivityEvent
import com.trello.rxlifecycle.android.RxLifecycleAndroid
import com.karaoke.android.library.mvvm.MvvmViewModel
import rx.Observable
import rx.subjects.BehaviorSubject

/**
 * Created by Administrator on 2016/12/2.
 */
abstract class ActivityViewModel : MvvmViewModel, LifecycleProvider<ActivityEvent>, BaseObservable()
{

    private val lifecycleSubject = BehaviorSubject.create<ActivityEvent>()

    override fun lifecycle(): Observable<ActivityEvent>
    {
        return lifecycleSubject.asObservable()
    }

    override fun <T> bindUntilEvent(event: ActivityEvent): LifecycleTransformer<T>
    {
        return RxLifecycle.bindUntilEvent<T, ActivityEvent>(lifecycleSubject, event)
    }

    override fun <T> bindToLifecycle(): LifecycleTransformer<T>
    {
        return RxLifecycleAndroid.bindActivity<T>(lifecycleSubject)
    }

    override fun onCreate()
    {
        lifecycleSubject.onNext(ActivityEvent.CREATE)
    }

    override fun onStart()
    {
        lifecycleSubject.onNext(ActivityEvent.START)
    }


    override fun onResume()
    {
        lifecycleSubject.onNext(ActivityEvent.RESUME)
    }

    override fun onPause()
    {
        lifecycleSubject.onNext(ActivityEvent.PAUSE)
    }

    override fun onDestroy()
    {
        lifecycleSubject.onNext(ActivityEvent.DESTROY)
    }

    override fun onStop()
    {
        lifecycleSubject.onNext(ActivityEvent.STOP)
    }
}
